package com.zyf.chapter02.creation.builder;

import com.zyf.chapter01.principle.DRY.utils.StringUtils;

/**
 * 使用构造函数初始化资源池配置
 * @author yifeng
 */
public class V3ResourcePoolConfig {
    private static final int DEFAULT_MAX_TOTAL = 8;
    private static final int DEFAULT_MAX_IDLE = 8;
    private static final int DEFAULT_MIN_IDLE = 0;

    /**
     * 必填
     * 默认值：没有
     */
    private String name;
    /**
     * 非必填
     * 默认值 8
     */
    private int maxTotal = DEFAULT_MAX_TOTAL;
    /**
     * 非必填
     * 默认值 8
     */
    private int maxIdle = DEFAULT_MAX_IDLE;
    /**
     * 非必填
     * 默认值 0
     */
    private int minIdle = DEFAULT_MIN_IDLE;

    private V3ResourcePoolConfig(Builder builder) {
        this.name = builder.name;
        this.maxIdle = builder.maxIdle;
        this.maxTotal = builder.maxTotal;
        this.minIdle = builder.minIdle;

    }

    public static Builder builder() {
        return new Builder();
    }

    public static class Builder {
        /**
         * 必填
         * 默认值：没有
         */
        private String name;
        /**
         * 非必填
         * 默认值 8
         */
        private int maxTotal = DEFAULT_MAX_TOTAL;
        /**
         * 非必填
         * 默认值 8
         */
        private int maxIdle = DEFAULT_MAX_IDLE;
        /**
         * 非必填
         * 默认值 0
         */
        private int minIdle = DEFAULT_MIN_IDLE;

        public V3ResourcePoolConfig build() {
            // 校验逻辑放入此处
            if (name == null) {
                throw new IllegalArgumentException("...");
            }
            if (maxIdle > maxTotal) {
                throw new IllegalArgumentException("...");
            }
            if (minIdle > maxTotal || minIdle > maxIdle) {
                throw new IllegalArgumentException("...");
            }

            return new V3ResourcePoolConfig(this);
        }

        public Builder setName(String name) {
            this.name = name;
            return this;
        }

        public Builder setMaxTotal(Integer maxTotal) {
            if (maxTotal != null) {
                if (maxTotal <= 0) {
                    throw new IllegalArgumentException("maxTotal should be positive.");
                }
                this.maxTotal = maxTotal;
            }
            return this;
        }

        public Builder setMaxIdle(Integer maxIdle) {
            if (maxIdle != null) {
                if (maxIdle <= 0) {
                    throw new IllegalArgumentException("maxIdle should not be negative.");
                }
                this.maxIdle = maxIdle;
            }
            return this;
        }

        public Builder setMinIdle(Integer minIdle) {
            if (minIdle != null) {
                if (minIdle <= 0) {
                    throw new IllegalArgumentException("minIdle should not be negative.");
                }
                this.minIdle = minIdle;
            }
            return this;
        }
    }

    @Override
    public String toString() {
        return "V3ResourcePoolConfig{" +
                "name='" + name + '\'' +
                ", maxTotal=" + maxTotal +
                ", maxIdle=" + maxIdle +
                ", minIdle=" + minIdle +
                '}';
    }

    public static void main(String[] args) {
        // 使用举例
        V3ResourcePoolConfig config = V3ResourcePoolConfig.builder().setName("guava").setMaxIdle(5).build();
        System.out.println(config);
    }
}
